package com.tmc.util;

import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.intcar.EthPrice;
import com.tmc.bean.intcar.EthQueryPriceReq;
import com.tmc.constant.CacheConstant;
import com.tmc.service.cache.CacheServiceImpl;

/**
 * @author zhang
 * @date 2017年12月19日 下午4:04:40 
 */
public class IntcarUtil {
	
	public static String getCacheKey(ContextInfo context, String reqJSON){
		return "KEY_PRICING_" + context.getUserId() + reqJSON;
	}
	
	@SuppressWarnings("unchecked")
	public static CommonResult getPriceFromCache(ContextInfo context, String priceMark, CacheServiceImpl cache){
		if(ObjectUtil.noText(priceMark)){
			return CommonResult.makeFail("请重新查询！101");
		}
		Map<String, Object> map = (Map<String, Object>) cache.get(CacheConstant.KEY_PRICING_REQUEST_MAP);
		if(map == null || map.isEmpty()){
			return CommonResult.makeFail("超时，请重新查询！102");
		}
		if(map.get(priceMark) == null){
			return CommonResult.makeFail("超时，请重新查询！103");
		}
		
		EthPrice price = (EthPrice) cache.get(priceMark);
		
		if(price == null){
			return CommonResult.makeFail("超时，请重新查询！104");
		}
		// TODO 验价逻辑
		return CommonResult.makeSuccess("", price);
	}
	
	@SuppressWarnings("unchecked")
	public static CommonResult getEthPricingReqFromCache(ContextInfo context, String priceMark, CacheServiceImpl cache){
		Map<String, Object> map = (Map<String, Object>) cache.get(CacheConstant.KEY_PRICING_REQUEST_MAP);
		if(map == null || map.isEmpty()){
			return CommonResult.makeFail("超时，请重新查询！102");
		}
		if(map.get(priceMark) == null){
			return CommonResult.makeFail("超时，请重新查询！103");
		}
		String reqJSON = (String) map.get(priceMark);
		
		return CommonResult.makeSuccess("", JSON.parseObject(reqJSON, EthQueryPriceReq.class));
	}
}